package com.shhm.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.shhm.admin.mapper.RoleMapper;
import com.shhm.admin.mapper.UserRoleMapper;
import com.shhm.admin.service.RoleService;
import com.shhm.admin.util.RequestUtil;
import com.shhm.common.dto.request.RequestList;
import com.shhm.common.dto.response.AjaxResultPlus;
import com.shhm.common.entity.admin.RoleEntity;
import com.shhm.common.entity.admin.SysRoleOperatePerms;
import com.shhm.common.mapper.SysRoleOperatePermsMapper;
import com.shhm.common.utils.MybatisPlusUtil;
import com.shhm.common.utils.RedisUtil;
import com.shhm.common.utils.ResponseUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/8/14 下午3:41
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private SysRoleOperatePermsMapper roleOperatePermsMapper;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public AjaxResultPlus pageList(Integer pageNum, Integer pageSize,Integer isPage, HttpServletRequest request) {
        AjaxResultPlus res = AjaxResultPlus.custom(200, false);
        if (isPage == 0) {
            LambdaQueryWrapper<RoleEntity> wq = new LambdaQueryWrapper<RoleEntity>().select(RoleEntity::getRoleId, RoleEntity::getRoleName);
            List<RoleEntity> list = roleMapper.selectList(wq);
            return res.put("data",list);
        }
        PageDTO<RoleEntity> userPage = new PageDTO<>(pageNum, pageSize);
        String userOperatePerms = RequestUtil.getUserOperatePerms(request, roleOperatePermsMapper);
//        if (userOperatePerms.equals("NOT_PERMISSION")) return AjaxResultPlus.error(403,"您无权查看数据");
        PageDTO<RoleEntity> list= roleMapper.selectPage(userPage, null);
        return AjaxResultPlus.custom(200, false)
                .put("data",list)
                .put("operateStr",userOperatePerms);
    }

    @Override
    public AjaxResultPlus pageSearchList(Integer pageNum, Integer pageSize, RoleEntity entity) {
        PageDTO<RoleEntity> userPage = new PageDTO<>(pageNum, pageSize);
        QueryWrapper<RoleEntity> qw = MybatisPlusUtil.buildSearchQueryWrapper(entity);
        PageDTO<RoleEntity> list = roleMapper.selectPage(userPage, qw);
        return AjaxResultPlus.success(list,false);
    }

    @Transactional
    @Override
    public AjaxResultPlus add(RoleEntity entity) {
        entity.setCreateTime(LocalDateTime.now());
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(roleMapper.insert(entity));
        flushRedisAboutRole();
        return res;
    }

    @Transactional
    @Override
    public AjaxResultPlus update(RoleEntity entity) {
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(roleMapper.updateById(entity));
        flushRedisAboutRole();
        return res;
    }

    @Transactional
    @Override
    public AjaxResultPlus delete(RequestList ids) {
        List<Object> list = ids.getList();
        userRoleMapper.resetUserRole(list);
        roleOperatePermsMapper.resetUserRole(list);
        AjaxResultPlus res = ResponseUtil.sqlExeIsSuccess(roleMapper.deleteByIds(list));
//        RedisUtil.removePrefixData(redisTemplate,RedisUtil.REDIS_OPERATE_PERMS_KEY);
        flushRedisAboutRole();
        return res;
    }

    private void flushRedisAboutRole(){
        List<SysRoleOperatePerms> list = roleOperatePermsMapper.selectRoleOperatePerms();
        RedisUtil.flushSysRoleOperatePerms(list,redisTemplate);
    }
}
